/*
 * @Author: taoyuqing
 * @Date: 2022-12-23 00:57:55
 * @Description: 首页
 */
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:spree/pages/drawerContent/drawer_content.dart';

import '../utils/utils.dart';
import '../widgets/banner_slider.dart';
import '../widgets/gameGroup/game_group.dart';
import '../widgets/menu_bar.dart';
import '../widgets/menu_games.dart';

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin{
  late ScrollController _scrollController;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController = ScrollController();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: SvgPicture.asset('images/BETSPREELOGO.svg'),
      ),
      drawer: Drawer(
        backgroundColor: Color(0xFF372E6F),
        child: DrawerContent(
          onItemTap: (type) {
            Navigator.pop(context);
            _scrollPosition(type);
          },
        ),
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        color: const Color(0xFF100A2A),
        padding: const EdgeInsets.fromLTRB(20, 0, 20, 5),
        child: ListView(
          controller: _scrollController,
          children: [
            Container(
              padding: const EdgeInsets.only(top: 20),
              child: BannerSlider(),
            ),
            Container(
              margin: const EdgeInsets.only(top: 20),
              child: MenuBar(
                onItemTap: (type) {
                  _menuBarClick(type);
                },
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 20),
              child: const MenuGames(),
            ),
            Container(
              margin: const EdgeInsets.only(top: 40),
              child: GameGroup(
                iconPath: 'images/game-title1.png',
                groupName: 'Original Games',
                onTapRight: _handleAllGames,
                onTap: (data) {
                  try {
                    Navigator.pushNamed(context, data['name']);
                  } catch (e) {}
                },
                games: [
                  {'img': 'images/game7.png', 'name': 'Crash'},
                  {'img': 'images/game8.png', 'name': 'Dice'},
                  {'img': 'images/game9.png', 'name': 'Mines'},
                  {'img': 'images/game10.png', 'name': 'Double'},
                  {'img': 'images/game11.png'},
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 40),
              child: GameGroup(
                onTapRight: _handleAllGames,
                iconPath: 'images/game-title2.png',
                groupName: 'Live Casino',
                games: [
                  'images/game12.png',
                  'images/game13.png',
                  'images/game14.png',
                  'images/game15.png',
                  'images/game16.png'
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 40),
              child: GameGroup(
                onTapRight: _handleAllGames,
                iconPath: 'images/game-title3.png',
                groupName: 'New',
                games: [
                  'images/game17.png',
                  'images/game18.png',
                  'images/game19.png',
                  'images/game20.png',
                  'images/game21.png'
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  _handleAllGames() {
    Navigator.pushNamed(context, 'GameSearchPage');
  }

  _scrollPosition(String type) {
    switch (type) {
      case 'games':
        _scrollController.animateTo(700,
            duration: Duration(milliseconds: 300), curve: Curves.ease);
        break;
      case 'live':
        _scrollController.animateTo(1060,
            duration: Duration(milliseconds: 300), curve: Curves.ease);
        break;
      default:
    }
  }

  _menuBarClick(type) {
    switch (type) {
      case 'live':
        _scrollController.animateTo(1060,
            duration: Duration(milliseconds: 300), curve: Curves.ease);
        break;
      case 'games':
        _scrollController.animateTo(700,
            duration: Duration(milliseconds: 300), curve: Curves.ease);
        break;
      default:
    }
  }
  
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
